package com.ayu.scalalearn.chapter08

/**
  * @Author 徐林玉
  * @Description Test04MatchObject
  * @Date 2022/8/18 11:22
  * @Version 1.0
  */
object Test04MatchObject {
  def main(args: Array[String]): Unit = {


    val student = new StudentTest04("yy", 18)

    val res = student match {

      case StudentTest04("yy", 18) => "student yy"
      case _ => "else"
    }

    println(res)

  }

}

//定义类
class StudentTest04(val name: String, val age: Int)

//定义伴生对象

object StudentTest04 {
  def apply(name: String, age: Int): StudentTest04 = new StudentTest04(name, age)

  // 必须实现一个unapply方法，用来对对象属性进行拆解
  def unapply(arg: StudentTest04): Option[(String, Int)] = {


    if (arg == null) {

      None
    } else {
      Some(arg.name, arg.age)
    }
  }
}


